package com.example.tvidemo.controller;

import com.example.tvidemo.entity.User;
import com.example.tvidemo.rest.ResponseData;
import com.example.tvidemo.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/noaccess/test")
    public String test(){
        return "ok";
    }

    @ApiOperation(value="获取所有用户详细信息", notes="获取所有用户详细信息")
    @GetMapping("/noaccess/findAll")
    public ResponseData findAll(){
        List<User> users = userService.findAll();
        return new ResponseData(200,"请求成功",users);
    }

    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String",paramType="path")
    @GetMapping("/get/{id}")
    public ResponseData getUser(@PathVariable("id") String id){
        User user = userService.findByUserId(id);
        return new ResponseData(200,"请求成功",user);
    }

    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PostMapping("/add")
    public ResponseData addUser(@RequestBody User user){
        userService.addUser(user);
        return new ResponseData(200,"添加成功",user);
    }

    @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String",paramType="path")
    @DeleteMapping("/delete/{id}")
    public ResponseData delUser(@PathVariable("id") String id){
        userService.deleteUser(id);
        return new ResponseData(200,"删除成功",null);
    }

    @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String",paramType="path"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
    @PutMapping("/update/{id}")
    public ResponseData updateUser(@PathVariable("id") String id,@RequestBody User user){
        User updateUser = userService.findByUserId(id);
        updateUser.setName(user.getName());
        updateUser.setUsername(user.getUsername());
        updateUser.setPassword(user.getPassword());
        userService.updateUser(updateUser);
        return new ResponseData(200,"修改成功",null);
    }

}
